<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/11
 * Time: 17:28
 */
namespace app\admin\collection;
use think\Db;

class  chartCollection{

    /**
     * $year 年份
     * $user_id 用户ID
     * ['根据条件获取某会员月认证数']
     * @param $string
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function getAllMonthAuthentication($user_id, $condition,$year){
        $arr = [];
        for($i=0;$i<12;$i++){
            $time_arr = self::mFristAndLast($year,$i+1);

            $num = Db::table('tp_distribution')
                ->alias('d')
                ->field('d.customers_id')
                ->where("d.user_id",'=',$user_id)
                ->where("c.customer_levlel",'=',2)
                ->whereTime('d.addtime', 'between', [$time_arr['firstday'], $time_arr['lastday']])
                ->join('tp_customers c','d.customers_id = c.customers_id')
                ->count();
            $arr[$i] = $num;
        }
        return $arr;
    }

    /**
     * [根据条件获取某会员各种会员数]
     * @param $user_id
     * @param $condition
     * @param $year
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function getAllMonthMember($user_id, $condition,$year){
        $arr = [];
        for($i=0;$i<12;$i++){
            $arr[$i+1]['num'] = $arr[$i+1]['num1'] = $arr[$i+1]['num2'] = $arr[$i+1]['num3'] = $arr[$i+1]['num4'] = 0;
            $time_arr = self::mFristAndLast($year,$i+1);

            $list = Db::table('tp_distribution')
                ->alias('d')
                ->field('d.customers_id,c.customer_levlel')
                ->where("d.user_id",'=',$user_id)
                ->where("c.customer_levlel",'>',2)
                ->whereTime('d.addtime', 'between', [$time_arr['firstday'], $time_arr['lastday']])
                ->join('tp_customers c','d.customers_id = c.customers_id')
                ->select();
            $customer_levlel_arr = array_column($list,'customer_levlel');
            $arr[$i+1]['num'] = count($customer_levlel_arr);
            foreach($customer_levlel_arr as $v){
                switch ($v)
                {
                    case 3:
                        $arr[$i+1]['num1'] += 1;
                        break;
                    case 4:
                        $arr[$i+1]['num2'] += 1;
                        break;
                    case 5:
                        $arr[$i+1]['num3'] += 1;
                        break;
                    case 6:
                        $arr[$i+1]['num4'] += 1;
                        break;
                    default:
                }
            }
        }
        return $arr;
    }


    /**
     * 获取指定月份的第一天开始和最后一天结束的时间戳
     *
     * @param int $y 年份 $m 月份
     * @return array(本月开始时间，本月结束时间)
     */
    static public function mFristAndLast($y = "", $m = ""){
        if ($y == "") $y = date("Y");
        if ($m == "") $m = date("m");
        $m = sprintf("%02d", intval($m));
        $y = str_pad(intval($y), 4, "0", STR_PAD_RIGHT);

        $m>12 || $m<1 ? $m=1 : $m=$m;
        $firstday = strtotime($y . $m . "01000000");
        $firstdaystr = date("Y-m-01", $firstday);
        $lastday = strtotime(date('Y-m-d 23:59:59', strtotime("$firstdaystr +1 month -1 day")));

        return array(
            "firstday" => $firstday,
            "lastday" => $lastday
        );
    }

    /**
     * 获取月份业绩
     * @param $rz_arr
     * @param $_list
     * @return array
     * @throws \think\Exception
     */
    static public function getMonthYj($rz_arr, $_list){
        $level = getYj();
        $arr = [];
        for($i=0;$i<12;$i++){
            $arr[$i] = ($rz_arr[$i] * $level['sm']) + (intval($_list[$i+1]['num1']) * $level['zy']) + (intval($_list[$i+1]['num2']) * $level['gj']) + (intval($_list[$i+1]['num3']) * $level['hh']) + (intval($_list[$i+1]['num4']) * $level['qy']);
        }

        return $arr;
    }

}